Covariance estimation#
import pandas as pd
pd.options.plotting.backend = "plotly"
# Load historic price data of 20 stocks
prices=pd.read_csv("data/stock_prices.csv", header=0, index_col=0, parse_dates=True)
# Compute the historic returns
returns = prices.pct_change().dropna(axis=0, how="all")[["GOOG", "AAPL", "FB"]]
from cvx.covariance.combination import from_ewmas
# Pairs of halflife volatility vs halflife covariances
pairs = [(10, 10), (21, 21), (21, 63)]
combinations = from_ewmas(
returns,
pairs,
clip_at=4.2,
mean=True
)
weights = pd.DataFrame({result.time : result.weights for result in combinations.solve(window=10)}).transpose()
weights
| 10-10 | 21-21 | 21-63 | |
|---|---|---|---|
| 2017-03-15 | 1.000000e+00 | 2.408422e-09 | 2.495247e-09 |
| 2017-03-16 | 1.000000e+00 | 6.696406e-09 | 8.048516e-09 |
| 2017-03-17 | 1.000000e+00 | 5.090497e-09 | 6.028853e-09 |
| 2017-03-20 | 1.000000e+00 | 7.147203e-09 | 1.184262e-08 |
| 2017-03-21 | 1.393968e-08 | 1.000000e+00 | 3.310787e-08 |
| ... | ... | ... | ... |
| 2018-04-05 | 1.047505e-08 | 1.000000e+00 | 2.446955e-08 |
| 2018-04-06 | 7.062781e-09 | 1.000000e+00 | 1.658520e-08 |
| 2018-04-09 | 5.972326e-09 | 1.000000e+00 | 2.305724e-08 |
| 2018-04-10 | 2.909872e-07 | 9.999997e-01 | 2.763267e-08 |
| 2018-04-11 | 6.118023e-09 | 9.999999e-01 | 6.318126e-08 |
271 rows × 3 columns
weights.plot()